//package org.wangnian.corsprovider.config;
//
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.builders.WebSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.security.provisioning.InMemoryUserDetailsManager;
//import org.springframework.web.cors.CorsConfiguration;
//import org.springframework.web.cors.CorsConfigurationSource;
//import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
//
//import java.io.PrintWriter;
//import java.time.Duration;
//import java.util.Arrays;
//import java.util.Collections;
//
//@Configuration
//public class SecurityConfig extends WebSecurityConfigurerAdapter {
//
//    @Bean
//    PasswordEncoder passwordEncoder(){
//        return new BCryptPasswordEncoder();
//    }
//
//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        web.ignoring().antMatchers("/css/**","/images/**","/js/**");
//    }
//
//    @Override
//    @Bean
//    protected UserDetailsService userDetailsService() {
//        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
//        manager.createUser(User.withUsername("123")
//                .password(passwordEncoder().encode("123")).roles("admin").build());
//        return manager;
//    }
//
//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//
//        http.authorizeRequests().anyRequest().authenticated()
//                .and().formLogin()
//                .loginPage("/login.html")
//                .loginProcessingUrl("/doLogin")
//                .failureHandler((req, resp, e) -> {
//                    resp.setContentType("application/json;charset=utf-8");
//                    PrintWriter out = resp.getWriter();
//                    out.write(e.getMessage());
//                    out.flush();
//                    out.close();
//                })
//                .permitAll()
////                .and().cors().configurationSource(corsConfigurationSource())
//                .and().cors()
//                .and().csrf().disable();
//    }
//
////    @Bean
////    CorsConfigurationSource corsConfigurationSource(){
////        CorsConfiguration corsConfiguration = new CorsConfiguration();
////        corsConfiguration.setAllowCredentials(true);
////        // 设置访问源地址
////        corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
////        // 设置访问源请求方法
////        corsConfiguration.setAllowedMethods(Collections.singletonList("*"));
////        // 设置访问源请求头
////        corsConfiguration.setAllowedHeaders(Collections.singletonList("*"));
////        //有效期 1 h
////        corsConfiguration.setMaxAge(Duration.ofHours(1));
////        // 添加映射路径，拦截一切请求
////        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
////        source.registerCorsConfiguration("/**",corsConfiguration);
////        return source;
////    }
//}
